﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Windows.Documents;

namespace Notes
{   
    public partial class NoteEditor : Form
    {        
        private Note note;

        public Note EditedNote
        {
            get
            {
                return note;
            }
        }

        public NoteEditor(Note note)
        {
            InitializeComponent();
            this.note = note;
        }

        private void toolStripBold_Click(object sender, EventArgs e)
        {
            TextControl.TextFormatting.ToggleBold();
        }

        private void toolStripItalic_Click(object sender, EventArgs e)
        {
            TextControl.TextFormatting.ToggleItalics();
        }

        private void toolStripUnderline_Click(object sender, EventArgs e)
        {
            TextControl.TextFormatting.ToggleUnderline();
        }

        private void toolStripHighlight_Click(object sender, EventArgs e)
        {            
        }

        private void toolStripLink_Click(object sender, EventArgs e)
        {
            TextControl.InsertHyperlink(null, null);
        }

        private void italicToolStripMenuItem_Click(object sender, EventArgs e)
        {
            toolStripItalic.Checked = toolStripItalic.Checked;
        }

        private void underlineToolStripMenuItem_Click(object sender, EventArgs e)
        {
            toolStripUnderline.Checked = toolStripUnderline.Checked;
        }

        private void boldToolStripMenuItem_Click(object sender, EventArgs e)
        {
            toolStripBold.Checked = toolStripBold.Checked;
        }

        private void highlightToolStripMenuItem_Click(object sender, EventArgs e)
        {
            toolStripHighlight.Checked = toolStripHighlight.Checked;
        }

        private void OK_Click(object sender, EventArgs e)
        {
            note.Tags = TagsControl.Text;
            note.Title = TitleControl.Text;
            note.Text = TextControl.SaveHtml();
        }

        private void ResizeToFit()
        {
            Cancel.Location = new Point(ClientSize.Width - Cancel.Size.Width - 20,
                                        ClientSize.Height - Cancel.Size.Height - Style.BORDER.Height);
            OK.Location = new Point(Cancel.Location.X - OK.Size.Width - 6,
                                    Cancel.Location.Y);

            TagLabel.Location = new Point(0, OK.Location.Y - TagsControl.Size.Height);
            TagsControl.Location = new Point(TagLabel.Location.X + TagLabel.Size.Width + Style.BORDER.Width,
                                             OK.Location.Y - TagsControl.Size.Height - Style.BORDER.Height);
            var sz = TagsControl.Size;
            TagsControl.Size = new Size(ClientSize.Width - TagLabel.Size.Width - Style.BORDER.Width * 2, sz.Height);
                
            TitleControl.Location = new Point(0, toolStrip1.Size.Height);
            sz = TitleControl.Size;
            TitleControl.Size = new Size(ClientSize.Width, sz.Height);

            TextControl.Location = new Point(0, TitleControl.Location.Y + TitleControl.Size.Height + Style.BORDER.Height);
            var height = TagsControl.Location.Y - Style.BORDER.Height;
            height -= TitleControl.Location.Y + TitleControl.Size.Height + Style.BORDER.Height;

            TextControl.Size = new Size(ClientSize.Width, height);
        }

        private void NoteEditor_Layout(object sender, LayoutEventArgs e)
        {
            ResizeToFit();
        }

        private void TextControl_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                e.IsInputKey = true;
            }
        }

        void mnuItemLink_Click(object sender, System.EventArgs e)
        {
            TextControl.InsertHyperlink(null, null);
        }
    }
}
